package com.gitee.sop.websiteserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.gitee.sop.websiteserver.bean.Result;
import com.gitee.sop.websiteserver.entity.FileConfig;
import com.gitee.sop.websiteserver.service.FileConfigHandler;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 文件配置接口
 *
 * @author admin
 */
@RestController
@RequestMapping("portal/fileConfig")
public class FileConfigController {

    @Resource
    private FileConfigHandler fileConfigHandler;


    @PostMapping("/add")
    public Result config(@RequestBody @Valid FileConfig fileConfig) {
        fileConfigHandler.addOrUpdate(fileConfig);
        return Result.ok();
    }

    @GetMapping("/get")
    public Result get(@RequestParam String appKey) {
        JSONObject jsonObject = fileConfigHandler.checkAndGetConfig(appKey);
        String id = jsonObject.get("_id").toString();
        jsonObject.put("_id", id);
        return Result.ok(jsonObject);
    }

}
